<?php
// @formatter:off
/**
 * @file WCUserData.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-05-24 23:14:00 +0000 (Fri, 24 May 2013) $
 *
 * $Id: WCUserData.php 66 2013-05-24 23:14:00Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA2/includes/representations/WCUserData.php $
 */
// @formatter:on

/**
 * @class WCUserData
 */
class WCUserData extends WCTableRepresentation {
	protected $_PropertiesReadOnly = array("id");
	protected $_PropertiesBoolean = array();
	protected $_PropertiesPrefix = "usd_";
	protected $_PropertiesTable = "user_data";
	protected $_PropertiesNameColumn = "fullname";
	protected $_PropertiesNULL = array(
		"email",
		"extra",
		"fullname"
	);
	protected $_PropertiesPresetFunc = array("extra" => "serialize");

	/**
	 * @var string
	 */
	protected $_profileCode = false;

	public function __construct() {
		parent::__construct();
	}
	// Public methods.
	public function avatar() {
		$out = "";

		global $wcDefaults;

		if(in_array($this->id(), array(
			WC_USER_ROOT_ID,
			WC_USER_CRON_ID
		))) {
			$out = "{$wcDefaults["avatar-basic"]}/root.png";
		} else {
			$avatar = explode("|", $this->avatar);
			switch(strtoupper($avatar[0])) {
				case "B":
					// Basic
					$out = "{$wcDefaults["avatar-basic"]}/{$avatar[1]}";
					break;
				case "T":
					// By theme
					$out = "{$wcDefaults["avatar-theme"]}/{$avatar[1]}";
					break;
				case "U":
				case "C":
					// By user
					$out = "{$wcDefaults["avatar-cache"]}/{$this->id()}{$avatar[1]}";
					break;
				default:
					$out = $wcDefaults["preview"];
					break;
			}
		}

		return $out;
	}
	public function fullname() {
		return $this->fullname;
	}
	public function getExtra($name) {
		global $wcUserXData;

		$out = false;
		$extraInfo = $this->extra;

		if(!in_array($name, $wcUserXData["_names"])) {
			trigger_error("'{$name}' is not defined as a valid extra user datum.", E_USER_ERROR);
		}
		if(!isset($extraInfo[$name])) {
			$default = false;
			foreach($wcUserXData as $key => $value) {
				if(is_numeric($key) && $value["name"] == $name) {
					$this->setExtra($name, $value["default"]);
					$out = $value["default"];
					break;
				}
			}
		} else {
			$out = $extraInfo[$name];
		}

		return $out;
	}
	public function load($id) {
		parent::load($id);

		if($this->ok()) {
			$this->_info["usd_extra"] = unserialize($this->_info["usd_extra"]);
		}

		return $this->ok();
	}
	public function setExtra($name, $value) {
		global $wcUserXData;

		$extraInfo = $this->extra;

		if(!in_array($name, $wcUserXData["_names"])) {
			trigger_error("'{$name}' is not defined as a valid extra user datum.", E_USER_ERROR);
		}

		$extraInfo[$name] = $value;
		$this->extra = $extraInfo;

		return $this->getExtra($name);
	}
}
?>
